//
//  RetrievepasswordViewController.m
//  Molian
//
//  Created by 胡津 on 15/4/16.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "RetrievepasswordViewController.h"
#import "MLLoginTextField.h"
@interface RetrievepasswordViewController ()
@property (strong, nonatomic) MLLoginTextField *passwordOne;
@property (strong, nonatomic) MLLoginTextField *passwordTwo;

@end

@implementation RetrievepasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"创建新密码";
    self.view.backgroundColor = [UIColor whiteColor];
    
    // Do any additional setup after loading the view.
    self.passwordOne = [[MLLoginTextField alloc]initWithFrame:CGRectMake(0, 80, DeviceWidth, 40)];
    self.passwordOne.placeholder = @"请设置不少于6位数的密码";
    self.passwordOne.secureTextEntry = YES;
    self.passwordOne.leftPrompt = @"    新密码     ";
    [self.view addSubview:self.passwordOne];
    [self.passwordOne style];
    
    self.passwordTwo = [[MLLoginTextField alloc]initWithFrame:CGRectMake(0, 130, DeviceWidth, 40)];
    self.passwordTwo.placeholder = @"请重复输入刚刚的密码";
    self.passwordTwo.secureTextEntry = YES;
    self.passwordTwo.leftPrompt = @" 重新输入密码  ";
    [self.view addSubview:self.passwordTwo];
    [self.passwordTwo style];

    
    UILabel *prompt = [[UILabel alloc]initWithFrame:CGRectMake(20, 180, DeviceWidth-40, 20)];
    prompt.text = @" *密码请勿填写得太简单";
    prompt.textColor = [UIColor grayColor];
    prompt.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:prompt];
    
    BFPaperButton *Register = [BFPaperButton buttonWithType:UIButtonTypeCustom];
    Register.frame = CGRectMake(20, 210, DeviceWidth-40, 40);
    [Register setTitle:@"确定" forState:UIControlStateNormal];
    [Register setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    Register.layer.masksToBounds = YES;//开启圆角
    [Register setBackgroundImage:[UIImage imageWithColor:APPCOLOR(1)] forState:UIControlStateNormal];
    [Register.layer setCornerRadius:CGRectGetHeight([Register bounds]) / 2];
    Register.titleLabel.font = [UIFont fontWithName:FontName size:14];
    [Register addTarget:self action:@selector(OK) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:Register];
    
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(returnKeyBorad)]];
}

-(void)returnKeyBorad
{
    [self.view endEditing:YES];
}
-(void)OK
{
    [self.view endEditing:YES];
    
    BOOL isParamValid = [self verifyPassword1:self.passwordOne.text password:self.passwordTwo.text];
    if (!isParamValid) {
        return;
    }
    
    // 附带验证码创建新密码
    NSString *password = self.passwordTwo.text;
    if ([password isEqualToString:@""]) {
        [HUD Prompt:@"请输入新密码"];
        return;
    }
    [MLUser resetPasswordWithSmsCode:self.verifyCode newPassword:password block:^(BOOL succeeded, NSError *error) {
        [HUD hideFromViewAnimated:YES];
        if (!error) {
            [HUD Prompt:@"修改密码成功"];
            // 修改密码成功,跳转到登录页面
            [self.navigationController popToRootViewControllerAnimated:YES];
        } else {
            NSString *msg = @"设置新密码错误";
            if (AVOS_RES_CODE_SMS_CODE_INVALID == error.code) {
                msg = @"验证码错误";
                [self.navigationController popViewControllerAnimated:YES];
            }
            [HUD Prompt:msg ];
        }
    }];
    [HUD setOnViewWithTitle:nil animated:YES];
    
}

- (BOOL)verifyPassword1:(NSString *)Password1 password:(NSString *)password {
    if (![self.passwordOne.text isEqualToString:self.passwordTwo.text]){
        [HUD Prompt:@"两次输入的密码不一致"];
        return NO;
    }else if (password.length < 6 || password.length > 16) {
        [HUD Prompt:@"密码应该是6-16个字符"];
        return NO;
    }else if (![self validatepassword:self.passwordTwo.text]){
        [HUD Prompt:@"密码过于简单"];
        return NO;
    }
    return YES;
}

-(BOOL) validatepassword:(NSString *)password
{
    NSString *pwdRegex = @"^[a-zA-Z0-9]{6,16}+$";
    NSPredicate *pwdTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pwdRegex];
    return [pwdTest evaluateWithObject:password];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}
@end
